<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片组件 -->
    <el-card class="box-card">
      <!-- 搜索和按钮区域 -->
      <el-row :gutter="20">
        <!-- 添加商品 -->
        <el-col :span="8">
          <el-input
            v-model="queryInfo.query"
            placeholder="请输入内容"
            class="input-with-select"
            clearable
            @clear="getGoodList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </el-col>
        <!-- 按钮 -->
        <el-col :span="4">
          <el-button type="primary" @click="addGood">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 数据表格 -->
      <el-table :data="goodsList" style="width: 100%" stripe border>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column align="center" label="商品价格(元)" prop="goods_price" width="100px"></el-table-column>
        <el-table-column align="center" label="商品重量" prop="goods_weight" width="100px"></el-table-column>
        <el-table-column align="center" label="创建时间" prop="add_time" width="150px"></el-table-column>
        <el-table-column align="center" label="操作" width="150px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="deleteGood(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'goodslist',
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 所有商品数据列表
      goodsList: [],
      // 商品总条数
      total: 0
    }
  },
  created () {
    this.getGoodList()
  },
  methods: {
    // 获取商品列表
    async getGoodList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$msg.error(res.meta.msg)

      // 对请求到的数据进行拦截，处理时间戳
      res.data.goods.forEach(item => {
        item.add_time = this.$dayjs(item.add_time).format('YYYY-MM-DD HH:mm')
      })

      this.goodsList = res.data.goods
      this.total = res.data.total
    },

    // 当每页显示条数发生改变时触发
    handleSizeChange (newPageSize) {
      this.queryInfo.pagesize = newPageSize
      this.getGoodList()
    },

    // 当页码发生改变时触发
    handleCurrentChange (pagenum) {
      this.queryInfo.pagenum = pagenum
      this.getGoodList()
    },

    // 点击删除按钮，删除商品
    async deleteGood (goodId) {
      const result = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (result !== 'confirm') return this.$msg.info('取消了删除')

      const { data: res } = await this.$http.delete(`goods/${goodId}`)
      if (res.meta.status !== 200) return this.$msg.error(res.meta.msg)
      this.$msg.success(res.meta.msg)
      this.getGoodList()
    },
    // 点击添加商品按钮，跳转到添加商品页面
    addGood () {
      this.$router.push('/good/addgood')
    }
  }
}
</script>

<style lang="less" scoped>
.el-table-column {
  text-align: center;
}
</style>
